package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final UserMapper userMapper;

    @Override
    public void deduct(Long id, Integer amount) {
        User user = getById(id);
        if (user == null || user.getStatus() == UserStatus.FREEZE) {
            throw new RuntimeException("用户有误");
        }
        if (amount > user.getBalance()) {
            throw new RuntimeException("余额不足");
        }
//        userMapper.decuctBalance(id,amount);
        int remain = user.getBalance() - amount;
        lambdaUpdate()
                .set(User::getBalance, remain)
                .set(remain == 0, User::getStatus, 2)
                .eq(User::getId, id)
                .update();
    }

    @Override
    public List<UserVO> getList(UserQuery userQuery) {
        String name = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer minBalance = userQuery.getMinBalance();
        Integer maxBalance = userQuery.getMaxBalance();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        /*queryWrapper.lambda()
                .like(StrUtil.isNotBlank(name), User::getUsername, name)
                .eq(status!=null && status !=0, User::getStatus, status)
                .ge(minBalance!=null, User::getBalance, minBalance)
                .le(maxBalance!=null, User::getBalance, maxBalance);*/
        List<User> list = lambdaQuery()
                .like(StrUtil.isNotBlank(name), User::getUsername, name)
                .eq(status != null && status != 0, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
//        return BeanUtil.copyToList(list(queryWrapper), UserVO.class);
        return BeanUtil.copyToList(list, UserVO.class);
    }

    @Override
    public UserVO queryUserAndAddressById(Long id) {
        User user = getById(id);
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        List<Address> list = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, id)
                .list();
        List<AddressVO> voList = BeanUtil.copyToList(list, AddressVO.class);
        userVO.setAddressVO(voList);
        return userVO;
    }

    @Override
    public List<UserVO> queryUserAndAddressByIds(List<Long> ids) {
        List<User> users = listByIds(ids);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        for (UserVO userVO : userVOS) {
            List<Address> list = Db.lambdaQuery(Address.class)
                    .eq(Address::getUserId, userVO.getId())
                    .list();
            List<AddressVO> addressVOS = BeanUtil.copyToList(list, AddressVO.class);
            userVO.setAddressVO(addressVOS);
        }
        return userVOS;
    }
}
